package com.huilian.iotbox.mqtt.server.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.iotbox.mqtt.server.base.service.RabbitSenderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class RabbitSenderServiceImpl implements RabbitSenderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.listener.iotBoxDeviceOnlineOrOffline.exchange.name}")
    private String deviceOnlineOrOfflineExchangeName;
    @Value("${spring.rabbitmq.listener.iotBoxDeviceOnlineOrOffline.key}")
    private String deviceOnlineOrOfflineRoutingKey;

    @Value("${spring.rabbitmq.listener.iotBoxDeviceHeartbeat.exchange.name}")
    private String deviceHeartbeatExchangeName;
    @Value("${spring.rabbitmq.listener.iotBoxDeviceHeartbeat.key}")
    private String deviceHeartbeatRoutingKey;

    @Override
    public void deviceOnlineOrOffline(DeviceVo deviceVo) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(deviceOnlineOrOfflineExchangeName, deviceOnlineOrOfflineRoutingKey, JSON.toJSONString(deviceVo), correlationData);
        log.info("--------deviceOnlineOrOffline发送MQ成功！--------时间{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Override
    public void deviceHeartbeat(JSONObject deviceStatus) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(deviceHeartbeatExchangeName, deviceHeartbeatRoutingKey, deviceStatus, correlationData);
        log.info("--------deviceHeartbeat发送MQ成功！--------时间{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Override
    public void disposeMotorTestingResult(DeviceVo deviceVo) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("iotBox-motorTesting-result", "iotBox-motorTesting-result.dispose", deviceVo, correlationData);
        log.info("--------发送MQ成功！--------时间{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    //定义消息声明
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            String outTradeNo = correlationData.getId();
            if (!ack) {
                log.error("订单：{},发送失败", correlationData.getId());
                log.error("MQ订单发送失败，Confirm插入数据库");
            }
            log.info("RabbitMQ correlationDataID：{}MQ 发送成功！", outTradeNo);
        }
    };

    //定义消息返回
    final RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode,
                                    String replyTest, String exchange, String routingKey) {
            System.err.println("return: " + exchange + ",routingKey:" + routingKey + ",replyCode: " + replyCode + ",replyTest:" + replyTest);
            log.error("订单：{},发送失败", message);
            log.error("MQ订单发送失败，Confirm插入数据库");
        }
    };
}
